package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.util.EmployeeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void save(CustomerTransfer customerTransfer) {
        Employee employee = EmployeeUtil.getEmployee();
        customerTransfer.setOperator(employee);
        customerTransfer.setOperateTime(new Date());
        customerMapper.updateByNewSeller(customerTransfer.getNewSeller(), customerTransfer.getCustomer());
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public void delete(Long id) {
        customerTransferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {
        customerMapper.updateStatus(Customer.STATUS_COMMON, customerTransfer.getCustomer().getId());
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> listAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public PageInfo<CustomerTransfer> query(QueryObject qo) {
        Employee employee = EmployeeUtil.getEmployee();
        List<Role> roles = roleMapper.selectSnByEmployee(employee.getId());
        for (Role role : roles) {

            if (!employee.isAdmin() && !role.getSn().equals("Market_Manager")) {
                qo.setSellerEId(employee.getId());
            }
        }

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "ct.operate_time DESC"); //对下一句sql进行自动分页
        List<CustomerTransfer> customerTransfers = customerTransferMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<>(customerTransfers);
    }

    @Override
    public void updateByAbsorb(CustomerTransfer customerTransfer) {
        customerMapper.updateStatus(Customer.STATUS_COMMON, customerTransfer.getCustomer().getId());
        customerTransferMapper.updateByAbsorb(customerTransfer.getReason(), customerTransfer.getId());
    }

}
